module Kuztuscms
  class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :encryptable, :validatable, :registerable and :omniauthable
    devise :database_authenticatable, :recoverable, :timeoutable, :rememberable, 
      :trackable, :lockable, :token_authenticatable, :validatable, :authentication_keys => [:username]

    # Setup accessible (or protected) attributes for your model
    attr_accessible :username, :email, :password, :password_confirmation, :remember_me

    validates_presence_of :username
    validates_uniqueness_of :username

    def self.find_for_database_authentication(warden_conditions)
      conditions = warden_conditions.dup
      username = conditions.delete(:username)
      where(conditions).where(["lower(username) = :value", { :value => username.strip.downcase }]).first
    end
  end
end
